package com.woniu.zsd.merchant.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.dto.NoticeListDto;
import com.woniu.form.PublishNoticeForm;
import com.woniu.form.QueryNoticeForm;
import com.woniu.form.UpdateNoticeForm;
import com.woniu.zsd.commons.core.model.Result;
import com.woniu.zsd.merchant.service.INoticeService;
import com.woniu.zsd.merchant.util.MerchartTokenUtil;
import com.woniu.zsd.permission.Permission.Permission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2025-03-27
 */
@RestController
@RequestMapping("/notice")
@Api(tags = "公告业务")
public class NoticeController {

    @Autowired
    private INoticeService noticeService;

    @Autowired
    private MerchartTokenUtil tokenUtil;

    @ApiOperation(value = "公告集合分页查询")
    @GetMapping("/list")
    public Result<Page<NoticeListDto>> noticeList(QueryNoticeForm queryNoticeForm){
        Page<NoticeListDto> page = noticeService.noticeList(queryNoticeForm);
        return new Result<Page<NoticeListDto>>().setCode(200).setMsg("查询公告成功").setData(page);
    }

    @ApiOperation(value = "公告发布")
    @PostMapping("/publish")
    @Permission("publishNotice")
    public Result publishNotice(@Valid @RequestBody PublishNoticeForm publishNoticeForm, @RequestHeader String token) {
        Integer adminId = tokenUtil.getAdminId(token);
        noticeService.publishNotice(publishNoticeForm, adminId);
        return Result.getResult().setCode(200).setMsg("公告发布成功");
    }

    @ApiOperation(value = "删除公告")
    @DeleteMapping("/delete")
    @Permission("deleteNotice")
    public Result deleteNotice(
            @ApiParam(value = "公告id列表") @RequestParam List<Integer> ids,
            @ApiParam(value = "发布人id") @RequestHeader String token
    ) {
        Integer adminId = tokenUtil.getAdminId(token);
        noticeService.deleteNotice(ids, adminId);
        return Result.getResult().setCode(200).setMsg("删除成功");
    }

    @ApiOperation(value = "修改公告")
    @PutMapping("/update")
    @Permission("updateNotice")
    public Result updateNotice(@Valid @RequestBody UpdateNoticeForm UpdateNoticeForm, @RequestHeader String token) {
        Integer adminId = tokenUtil.getAdminId(token);
        noticeService.updateNotice(UpdateNoticeForm, adminId);
        return Result.getResult().setCode(200).setMsg("修改成功");
    }

    @ApiOperation(value = "查询自己的草稿公告")
    @GetMapping("/draft")
    public Result<List<NoticeListDto>> draftNotice(@RequestHeader String token) {
        Integer adminId = tokenUtil.getAdminId(token);
        List<NoticeListDto> list = noticeService.draftNotice(adminId);
        return new  Result<List<NoticeListDto>>().setCode(200).setMsg("查询成功").setData(list);
    }
}
